package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strings"
)

import "unicode/utf8"

func main() {
	// fmt.Println(utf8.RuneCountInString("a"), utf8.RuneCountInString("啊"), utf8.RuneCountInString("啊a"))
	// fmt.Println(len("a"), len("啊啊"), len("啊a"))
	// fmt.Println("Hello, 世界", len("世界"), utf8.RuneCountInString("世界"))
	// fmt.Println(strings.Count("a", ""), strings.Count("啊", ""), strings.Count("我w", ""))
	// fmt.Println(strings.Count("a", ""), strings.Count("啊", ""), strings.Count("我w", ""))
	err := HandleText("D:\\code\\goApps\\src\\github.com\\wangbin\\jiebago\\dict.txt")
	if err != nil {
		panic(err)
	}
}

func HandleText(textfile string) error {
	file, err := os.Open(textfile)
	if err != nil {
		log.Printf("Cannot open text file: %s, err: [%v]", textfile, err)
		return err
	}
	defer file.Close()

	f, err := os.Create("dict1.txt")
	if err != nil {
		fmt.Printf("create map file error: %v\n", err)
		return err
	}
	defer f.Close()
	w := bufio.NewWriter(f)

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text() // or
		//line := scanner.Bytes()

		//do_your_function(line)
		// fmt.Printf("%s\n", line)
		arr := strings.Fields(line)
		if utf8.RuneCountInString(arr[0]) > 1 {
			lineStr := strings.Join(arr, " ")
			fmt.Fprintln(w, lineStr)
		}
	}
	return w.Flush()

	if err := scanner.Err(); err != nil {
		log.Printf("Cannot scanner text file: %s, err: [%v]", textfile, err)
		return err
	}

	return nil
}
